package com.kelaite.procurement.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GuardenExceptionHandler {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler
	@ResponseBody
	public BaseError handleCRMException(Exception ex) {
		BaseError errorInfo=new BaseError();
		errorInfo.setMessage(ex.getMessage());
		if(ex instanceof BaseRuntimeException){
			errorInfo.setCode(((BaseRuntimeException) ex).getErrorCode());			
			errorInfo.setExtraData(((BaseRuntimeException) ex).getExtraData());
			
		}else{
			//如果code 也是500，表明是未被系统处理的异常
			errorInfo.setCode(500);  
		}
		
		logger.error(ex.getMessage(),ex);
		return errorInfo;
    }	
	
}
